package com.dandandog.framework.common.utils;

import cn.hutool.http.useragent.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @author JohnnyLiu
 */
public final class SystemUtil {

    private static final String USER_AGENT = "User-Agent";

    private SystemUtil() {
        throw new AssertionError();
    }

    public static Optional<UserAgent> getUserAgent(HttpServletRequest request) {
        String userAgent = request.getHeader(USER_AGENT).toLowerCase();
        return Optional.ofNullable(UserAgentUtil.parse(userAgent));
    }

    public static Optional<Browser> getBrowser(HttpServletRequest request) {
        return getUserAgent(request).map(UserAgent::getBrowser);
    }

    public static Optional<String> getBrowserVersion(HttpServletRequest request) {
        return getUserAgent(request).map(UserAgent::getVersion);
    }

    public static Optional<OS> getOs(HttpServletRequest request) {
        return getUserAgent(request).map(UserAgent::getOs);
    }

    public static Optional<Platform> getPlatform(HttpServletRequest request) {
        return getUserAgent(request).map(UserAgent::getPlatform);
    }

    public static Optional<Engine> getEngine(HttpServletRequest request) {
        return getUserAgent(request).map(UserAgent::getEngine);
    }

    public static Optional<String> getEngineVersion(HttpServletRequest request) {
        return getUserAgent(request).map(UserAgent::getEngineVersion);
    }


}
